package cuit.cx.json;

import java.util.ArrayList;
import java.util.List;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cuit.cx.handlemodel.GrouponHandle;
import cuit.cx.model.Groupon;

public class GrouponHelper {
	
	public GrouponHandle doAnaylysis(String data) throws JSONException  {
		
		JSONObject jsonObject = new JSONObject(data);
		GrouponHandle grouponHandle = new GrouponHandle();
		grouponHandle.setResult(jsonObject.getInt("result"));
		List<Groupon> groupons = new ArrayList<Groupon>();
		
		JSONArray jsonArray = jsonObject.getJSONArray("groupons");
		
		for(int i=0 ;i<jsonArray.length();i++){
			JSONObject jsonObject2 = (JSONObject) jsonArray.opt(i);
			Groupon groupon = new Groupon();
			groupon.setId(jsonObject2.getInt("id"));
			groupon.setOldPrice((float)jsonObject2.getDouble("oldPrice"));
			groupon.setName(jsonObject2.getString("name"));
			groupon.setNowPrice((float)jsonObject2.getDouble("nowPrice"));
			groupon.setMainPictureUrl(jsonObject2.getString("mainPictureUrl"));
			groupons.add(groupon);
		}
		grouponHandle.setGroupons(groupons);
		
	/*	System.out.println("GrouponHandle--"+grouponHandle.getResult());
		for(int i=0 ;i<groupons.size();i++){
			Groupon groupon = groupons.get(i);
			System.out.println("GrouponHandle--"+groupon.getId());
		    System.out.println("GrouponHandle--"+groupon.getName());
			System.out.println("GrouponHandle--"+groupon.getNowPrice());
			System.out.println("GrouponHandle--"+groupon.getOldPrice());
		//	System.out.println("GrouponHandle--"+
		}*/
		
		return grouponHandle;
	}

}
